package com.huike.web.controller.system;

import com.huike.clues.domain.result.UserInfoAjaxResult;
import com.huike.clues.service.ISysDataService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictData;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "数据字典数据信息接口")
@Slf4j
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {
    @Resource
    ISysDataService iSysDataService;
    @PostMapping()
    @ApiOperation("新增字典类型")
    public UserInfoAjaxResult insertDict(@RequestBody SysDictData sysDictData){
        log.info("新增字典类型，操作人:{}", SecurityUtils.getUsername());
        iSysDataService.insertDict(sysDictData);
        return UserInfoAjaxResult.success();
    }
    @PutMapping()
    @ApiOperation("修改保存字典类型")
    public UserInfoAjaxResult updateDict(@RequestBody SysDictData sysDictData){
        log.info("修改保存字典类型，操作人:{},操作对象DictCode:{}", SecurityUtils.getUsername(),sysDictData.getDictCode());
        iSysDataService.updateDict(sysDictData);
        return UserInfoAjaxResult.success();
    }
    @GetMapping("/export")
    @ApiOperation("字典导出")
    public AjaxResult export(){
        log.info("字典导出，操作人:{}", SecurityUtils.getUsername());
        return iSysDataService.export();
    }
    @GetMapping("/list")
    @ApiOperation("字典列表")
    public TableDataInfo<List<SysDictData>> list(){
        log.info("字典列表，操作人:{}", SecurityUtils.getUsername());
        return iSysDataService.list();
    }
    @GetMapping("/type/{dictType}")
    @ApiOperation("根据字典类型查询字典数据信息")
    public TableDataInfo<List<SysDictData>> listByDictType(@PathVariable String dictType){
        log.info("根据字典类型查询字典数据信息，操作人:{},操作对象类型:{}", SecurityUtils.getUsername(),dictType);
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(dictType);
        return iSysDataService.listByDictType(sysDictData);
    }
    @DeleteMapping("/type/{dictType}")
    @ApiOperation("根据字典类型查询字典数据信息")
    public UserInfoAjaxResult deleteByDictType(@PathVariable String dictType){
        log.info("根据字典类型查询字典数据信息，操作人:{},操作对象类型:{}", SecurityUtils.getUsername(),dictType);
        iSysDataService.deleteByDictType(dictType);
        return UserInfoAjaxResult.success();
    }
    @GetMapping("/{dictCode}")
    @ApiOperation("根据字典类型查询字典数据信息")
    public TableDataInfo<List<SysDictData>> listByDictCode(@PathVariable String dictCode){
        log.info("根据字典类型查询字典数据信息，操作人:{},操作对象code:{}", SecurityUtils.getUsername(),dictCode);
        return iSysDataService.listByDictCode(dictCode);
    }
}
